package com.siwei.mes.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.Experiment;
import com.siwei.mes.entity.ExperimentSampleTake;
import com.siwei.mes.enums.ExperimentStatusEnum;
import com.siwei.mes.mapper.ExperimentMapper;
import com.siwei.mes.mapper.ExperimentSampleTakeMapper;
import com.siwei.mes.mapper.TglItemorderMapper;
import com.siwei.mes.service.AuthService;
import com.siwei.mes.service.ExperimentSampleTakeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账取样(ExperimentSampleTake)表服务实现类
 *
 * @author linzi
 * @since 2023-12-28 20:47:32
 */
@Service
public class ExperimentSampleTakeServiceImpl implements ExperimentSampleTakeService {
    @Resource
    private ExperimentSampleTakeMapper experimentSampleTakeMapper;
    @Resource
    private AuthService authService;
    @Resource
    private TglItemorderMapper tglItemorderMapper;
    @Resource
    private ExperimentMapper experimentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(ExperimentSampleTake entity) {
        assertTrue(tglItemorderMapper.selectByPrimaryKey(entity.getItemorderId()) == null, "tgl小票信息不存在");
        Experiment experiment = experimentMapper.loadById(entity.getExperimentId());
        assertTrue(experiment == null, "试验台账信息不存在");
        entity.setSamplePersonId(authService.getLoginUserId());
        entity.setSamplePersonName(authService.getLoginUserName());
        entity.setSampleTime(new Date());
        entity.init(true);
        experimentSampleTakeMapper.insertSelective(entity);

        /**需要修改对应试验台账的状态*/
        experiment.init(false);
        experiment.setExperimentStatus(ExperimentStatusEnum.IN_TEST.getCode());
        experimentMapper.updateByPrimaryKeySelective(experiment);
        return entity.getId();
    }

    @Override
    public void update(ExperimentSampleTake entity) {
        assertTrue(tglItemorderMapper.selectByPrimaryKey(entity.getItemorderId()) == null, "tgl小票信息不存在");
        assertTrue(experimentMapper.loadById(entity.getExperimentId()) == null, "试验台账信息不存在");
        ExperimentSampleTake experimentSampleTake = experimentSampleTakeMapper.selectByPrimaryKey(entity.getId());
        assertTrue(experimentSampleTake == null, "试验台账取样信息不存在");
        entity.init(false);
        experimentSampleTakeMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        ExperimentSampleTake experimentSampleTake = experimentSampleTakeMapper.selectByPrimaryKey(id);
        assertTrue(experimentSampleTake == null, "试验台账取样信息不存在");
        experimentSampleTakeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ExperimentSampleTake> getPage(PageQuery<ExperimentSampleTake, ExperimentSampleTake> query) {
        ExperimentSampleTake searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> experimentSampleTakeMapper.getList(searchParam));
    }

    @Override
    public ExperimentSampleTake loadById(Long id) {
        return experimentSampleTakeMapper.loadById(id);
    }

    @Override
    public List<ExperimentSampleTake> getAll(ExperimentSampleTake entity) {
        return experimentSampleTakeMapper.getList(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertOrUpdateByExperimentId(ExperimentSampleTake entity) {
        assertTrue(tglItemorderMapper.selectByPrimaryKey(entity.getItemorderId()) == null, "tgl小票信息不存在");
        Experiment experiment = experimentMapper.loadById(entity.getExperimentId());
        assertTrue(experiment == null, "试验台账信息不存在");

        Long experimentId = entity.getExperimentId();
        ExperimentSampleTake isExist = experimentSampleTakeMapper.selectByExperimentId(experimentId);

        if (isExist == null) {
            entity.setSamplePersonId(authService.getLoginUserId());
            entity.setSamplePersonName(authService.getLoginUserName());
            entity.setSampleTime(new Date());
            entity.init(true);
            experimentSampleTakeMapper.insertSelective(entity);
        } else {
            entity.setId(isExist.getId());
            entity.init(false);
            experimentSampleTakeMapper.updateByPrimaryKeySelective(entity);
        }

        /**需要修改对应试验台账的状态*/
        experiment.init(false);
        experiment.setExperimentStatus(ExperimentStatusEnum.IN_TEST.getCode());
        experimentMapper.updateByPrimaryKeySelective(experiment);
    }

    @Override
    public ExperimentSampleTake selectByExperimentId(Long experimentId) {
        return experimentSampleTakeMapper.selectByExperimentId(experimentId);
    }
}
